<?php
namespace app\api\controller;

use \think\Controller;
use \think\Cache;
class Test extends Controller
{
    public function index()
    {
        $user["menu"] = $this -> get_menu("GM");
        //$date = date("Y",time());
        $this -> assign("user", $user);
        //$this -> view -> engine -> layout(true);
        return $this -> fetch("/menu");
    }

    public function get_menu($role="")
    {
        $role = strtoupper($role);
        $menus = file_get_contents("../public/static/yiadmin/json/right.js");//菜单映射表
        $menus = json_decode($menus,true);//JSON转ARRAY

        //映射各角色能使用的菜单
        if($role == "FD"){$menus = $menus["FD"];}
        if($role == "OP"){$menus = $menus["OP"];}
        if($role == "GM"){$menus = $menus["GM"];}
        $menu_v = $menus;//纵向菜单，左边栏
        //横向菜单重组，顶部
        $menu_h = [];
        $menu_h_c = [];
        $menu_h_s = [];
        foreach ($menus as $key => $value) {
            if(isset($value['sub'])){
                // 有没有子菜单
                foreach ($value['sub'] as $k => $v) {
                    if($v["type"] == "contrl"){
                        array_push($menu_h_c, $v);
                    }
                    if($v["type"] == "select"){
                        array_push($menu_h_s, $v);
                    }
                }
            }else{
                // 没有子菜单
                if($value["type"] == "contrl"){
                    array_push($menu_h_c, $value);
                }
                if($value["type"] == "select"){
                    array_push($menu_h_s, $value);
                }
            }
        }
        $menu_h = array(   
            "menu_h_c" => $menu_h_c,
            "menu_h_s" => $menu_h_s
        );
        $html = array("menu_h"=>$menu_h,"menu_v"=>$menu_v);

        return $html;
    }
}